package com.rem.time.part1;

import org.junit.Test;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 新老版本时间处理对比
 *
 * @Author Rem
 * @Date 2020-02-15
 */

public class TestSimpleDateFormat {

    //以前传统时间版本存在线程安全问题


    //错误示范
    @Test
    public void test() throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Callable<Date> task = () -> sdf.parse("2020-02-15");

        ExecutorService pool = Executors.newFixedThreadPool(10);

        List<Future<Date>> results = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            results.add(pool.submit(task));
        }

        for (Future<Date> result : results) {
            System.out.println(result.get());
        }

        pool.shutdownNow();
    }

    //jdk1.8   LocalDate
    @Test
    public void test2() throws Exception {
        //  DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE;

        Callable<LocalDate> task = () -> LocalDate.parse("2020-02-15", dtf);

        ExecutorService pool = Executors.newFixedThreadPool(10);

        List<Future<LocalDate>> results = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            results.add(pool.submit(task));
        }

        for (Future<LocalDate> result : results) {
            System.out.println(result.get());
        }

        pool.shutdownNow();

    }
}
